var pageii = null;
var grid = null;
$(function () {
    grid = lyGrid({
        pagId: 'paging',
        l_column: [{
            colkey: "code",
            name: "机构商编码",
        }, {
            colkey: "name",
            name: "机构商名称"
        }, {
            colkey: "username",
            name: "手机号",
            width: '120px'
        }, {
            colkey: "area",
            name: "所属区域",
            renderData: function (rowindex, data, rowdata, column) {
                return rowdata.province + rowdata.city + rowdata.county;
            }
        }, {
            colkey: "status",
            name: "状态",
            renderData: function (rowindex, data, rowdata, column) {
                var ck = "";
                if (rowindex % 2) {
                    ck = "checked='checked'";
                }
                var str = "";
                if (data == "0") {
                    str = "正常";
                } else if (data == "1") {
                    str = "<span style='color: red;'>停用</span>";
                }
                var id = rowdata.id;
                var status = rowdata.status;
                return "<a href='javascript:toggleStatus(" + id + "," + status + ");'>" + str + "</a>";
            }
        }, {
            colkey: "can_deliver",
            name: "发货状态",
            renderData: function (rowindex, data, rowdata, column) {
                var str = "";
                if (data == "0") {
                    str = '不可发货';
                } else if (data == "1") {
                    str = '可发货';
                }
                var id = rowdata.id;
                var status = rowdata.can_deliver;
                return "<a href='javascript:toggleCanDeliver(" + id + "," + status + ");'>" + str + "</a>";
            }
        }, {
            colkey: "time",
            name: "开通时间",
            isSort: true,
            renderData: function (rowindex, data, rowdata, column) {
                return new Date(data).format("yyyy-MM-dd hh:mm:ss");
            }
        }],
        jsonUrl: rootPath + '/star/agent/findByPage.shtml',
        dymCol: true,
        checkbox: true,
        serNumber: true
    });
    $("#search").click("click", function () { // 绑定查询按扭
        var searchParams = $("#searchForm").serializeJson(); // 初始化传参数
        grid.setOptions({
            data: searchParams
        });
    });

    $("#addStarAgent").click("click", function () {
        addStarAgent();
    });
    $("#editStarAgent").click("click", function () {
        editStarAgent();
    });
});

function editStarAgent() {
    var cbox = grid.getSelectedCheckbox();
    var a = grid.selectRow();
    if (cbox.length > 1 || cbox == "") {
        layer.msg("请选中一条数据");
        return;
    }
    var id = a[0].id;
    pageii = layer.open({
        title: "编辑",
        type: 2,
        area: ["800px", "80%"],
        content: rootPath + '/star/agent/editUI.shtml?id=' + id
    });
}

function addStarAgent() {
    pageii = layer.open({
        title: "新增",
        type: 2,
        area: ["860px", "80%"],
        content: rootPath + '/star/agent/addUI.shtml'
    });
}

function toggleStatus(id, status) {
    layer.confirm('是否执行该操作？', function (index) {
        if (status == 1) {
            status = 0;
        } else if (status == 0) {
            status = 1;
        }
        var url = rootPath + '/star/agent/editStatus.shtml';
        var s = CommnUtil.ajax(url, {
            id: id,
            status: status
        }, "json");
        if (s == "success") {
            layer.msg('成功');
            grid.loadData();
        } else {
            layer.msg('失败');
        }
    });
}

function toggleCanDeliver(id, status) {
    layer.confirm('是否执行该操作？', function (index) {
        if (status == 1) {
            status = 0;
        } else if (status == 0) {
            status = 1;
        }
        var url = rootPath + '/star/agent/editCanDeliver.shtml';
        var s = CommnUtil.ajax(url, {
            id: id,
            status: status
        }, "json");
        if (s == "success") {
            layer.msg('成功');
            grid.loadData();
        } else {
            layer.msg('失败');
        }
    });
}
